<template>
  <el-main>

    <el-form :model="searchObject" label-width="80px" :inline="true" size="small">
      <el-form-item>
        <el-input v-model="searchObject.directorName" placeholder="请输入项目总监姓名" clearable/>
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchObject.selfIntroduction" placeholder="请输入自我介绍" clearable/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="selectList">查询</el-button>
        <el-button type="warning" icon="el-icon-refresh-right" @click="resetSearchObject">重置</el-button>
        <el-button type="success" icon="el-icon-plus" @click="openWindow">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" border stripe style="width: 100%; margin-bottom: 20px">

      <el-table-column prop="directorName" label="项目总监姓名"/>

      <el-table-column prop="sex" label="性别"/>

      <el-table-column prop="age" label="年龄" sortable/>

      <el-table-column align="center" label="本人头像" prop="selfAvatar">
        <template slot-scope="scope">
          <img style="width: 100%" :src="showImage(scope.$index,scope.row)">
        </template>
      </el-table-column>

      <el-table-column prop="selfIntroduction" label="自我介绍"/>

      <el-table-column prop="monthlySalary" label="月薪（元）" sortable/>

      <el-table-column label="操作选项" width="200" align="center">
        <template slot-scope="scope">
          <el-button icon="el-icon-edit-outline" type="primary" size="small" @click="handleEdit(scope.row)">编辑
          </el-button>
          <el-button icon="el-icon-close" type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>

    <system-dialog
      :title="dialog.title"
      :visible="dialog.visible"
      :width="dialog.width"
      :height="dialog.height"
      @onClose="onClose"
      @onConfirm="onConfirm">
      <div slot="content">
        <el-form :model="form" ref="form" :rules="rules"
                 label-width="80px" :inline="true" size="small">

          <el-form-item label="姓名" prop="directorName">
            <el-input v-model="form.directorName"></el-input>
          </el-form-item>

          <el-form-item label="性别" prop="sex">
            <el-radio v-model="form.sex" label="男">男</el-radio>
            <el-radio v-model="form.sex" label="女">女</el-radio>
          </el-form-item>

          <el-form-item label="年龄" prop="age">
            <el-input v-model.number="form.age"></el-input>
          </el-form-item>

          <el-form-item label="自我介绍" prop="selfIntroduction">
            <el-input type="textarea" v-model="form.selfIntroduction"></el-input>
          </el-form-item>

          <el-form-item label="本人头像" prop="selfAvatar">
            <el-upload
              class="avatar-uploader"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
              :data="uploadHeader"
              action="http://localhost:7777/file/upload?module=directorAvatar">
              <img v-if="form.selfAvatar" :src="form.selfAvatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>

          <el-form-item label="月薪（元）" prop="monthlySalary">
            <el-input v-model.number="form.monthlySalary"></el-input>
          </el-form-item>

        </el-form>
      </div>
    </system-dialog>

  </el-main>
</template>

<script>

    import director from "../../../api/director";
    import systemDialog from "../../../components/system/systemDialog";
    import {getToken} from '@/utils/auth';

    export default {
        name: "directorList",
        components: {
            systemDialog
        },
        data(){
            return{
                searchObject:{
                    directorName: "",
                    selfIntroduction: ""
                },
                tableData: [],
                form: {
                    id: "",
                    directorName: "",
                    sex: "",
                    age: "",
                    selfAvatar: "",
                    selfIntroduction: "",
                    monthlySalary: ""
                },
                dialog: {
                    title: "",
                    visible: false,
                    width: 600,
                    height: 200
                },
                rules: {
                    selfAvatar: [{required: true, message: '请上传一张图片作为头像', trigger: 'blur'}],
                    directorName: [{required: true, message: '请输入项目总监姓名', trigger: 'blur'}],
                    sex: [{required: true, message: '请选择性别', trigger: 'blur'}],
                    age: [{required: true, message: '请输入年龄', trigger: 'blur'}, {
                        type: 'number',
                        message: '年龄必须为数字值',
                        trigger: 'blur'
                    }],
                    selfIntroduction: [{required: true, message: '请输入自我介绍', trigger: 'blur'}],
                    monthlySalary: [{required: true, message: '请输入月薪（元）', trigger: 'blur'}, {
                        type: 'number',
                        message: '月薪必须为数字值',
                        trigger: 'blur'
                    }]
                },
                uploadHeader: {"token": getToken()}
            }
        },
        created() {
            this.selectList();
        },
        methods: {
            async selectList(){
                let res = await director.selectListOfDirector(this.searchObject);
                if(res.code === 200){
                    this.tableData = res.data;
                }
            },
            showImage(index, row) {
                return row.selfAvatar;
            },
            handleAvatarSuccess(res) {
                this.form.selfAvatar = res.data;
                this.$forceUpdate();
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isPNG = file.type === 'image/png';
                const limit = file.size / 1024 / 1024 < 5;

                if (!isJPG && !isPNG) {
                    this.$message.error('上传本人头像只能是 JPG 或 PNG 格式!');
                }
                if (!limit) {
                    this.$message.error('上传头像大小不能超过 5MB!');
                }
                return (isJPG || isPNG) && limit;
            },
            resetSearchObject(){
                this.searchObject = {};
                this.selectList();
            },
            openWindow(){
                this.$resetForm("form", this.form);
                this.dialog.title = "新增项目总监信息";
                this.dialog.visible = true;
            },
            onClose(){
                this.dialog.visible = false;
            },
            onConfirm(){
                this.$refs.form.validate(async (valid) => {
                    if (valid) {
                        let res = null;
                        if (this.form.id === "") {
                            res = await director.insert(this.form);
                        } else {
                            res = await director.update(this.form);
                        }
                        if (res.code === 200) {
                            this.$message.success(res.message);
                            this.selectList();
                            this.dialog.visible = false;
                        } else {
                            this.$message.error(res.message);
                        }
                    }
                });
            },
            handleEdit(row){
                this.$objCopy(row, this.form);
                this.dialog.title = "编辑项目总监信息";
                this.dialog.visible = true;
            },
            async handleDelete(row){
                let id = {id: row.id};
                let confirm = await this.$commonConfirm("执行此操作将无法撤销，确定删除吗？");
                if (confirm) {
                    let res = await director.delete(id);
                    if (res.code === 200) {
                        this.$message.success(res.message);
                        this.selectList();
                    } else {
                        this.$message.error(res.message);
                    }
                }
            }
        }
    }
</script>

<style lang="scss">
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9 !important;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar-uploader img {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
